package com.vca.front.controller;

import com.vca.common.page.CommonPage;
import com.vca.common.request.*;
import com.vca.common.response.*;
import com.vca.common.response.sf.ResponseCancel;
import com.vca.common.result.CommonResult;
import com.vca.service.service.InvoiceRecordService;
import com.vca.service.service.OrderService;
import com.vca.service.service.SFService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * H5端订单操作
 */
@Slf4j
@RestController("StoreOrderFrontController")
@RequestMapping("api/front/order")
@Api(tags = "订单")
public class StoreOrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private SFService sfService;


    @Autowired
    private InvoiceRecordService invoiceRecordService;

    /**
     * 订单列表
     * @param type 类型
     * @param pageRequest 分页
     * @return 订单列表
     */
    @ApiOperation(value = "订单列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiImplicitParams ({
        @ApiImplicitParam(name = "type", value = " 订单类型:1=课程/讲座/展览  2=礼品卡/纪念品/出版物", required = true)
    })
    public CommonResult<CommonPage<OrderDetailResponse>> orderList(@RequestHeader(value = "language",required = false) String language,
                                                                    @RequestParam(name = "type") Integer type,
                                                                   @ModelAttribute PageParamRequest pageRequest) {
        return CommonResult.success(orderService.list(language,type, pageRequest));
    }

    /**
     * 订单详情
     * @param orderNo 订单编号
     * @return 订单详情
     */
    @ApiOperation(value = "订单详情")
    @RequestMapping(value = "/detail/{orderNo}", method = RequestMethod.GET)
    public CommonResult<StoreOrderDetailInfoResponse> orderDetail(@RequestHeader(value = "language",required = false) String language,
                                                                    @PathVariable String orderNo) {
        return CommonResult.success(orderService.detailOrder(language,orderNo));
    }


    /**
     * 订单评价
     * @param request StoreProductReplyAddRequest 评论参数
     */
    @ApiOperation(value = "评价订单")
    @RequestMapping(value = "/comment", method = RequestMethod.POST)
    public CommonResult<Boolean> comment(@RequestBody @Validated StoreProductReplyAddRequest request) {
        if(orderService.reply(request)) {
            return CommonResult.success();
        }else{
            return CommonResult.failed();
        }
    }

    /**
     * 订单收货
     * @param orderNo Integer orderNo
     */
    @ApiOperation(value = "订单收货")
    @RequestMapping(value = "/take", method = RequestMethod.POST)
    public CommonResult<Boolean> take(@RequestParam(value = "orderNo") String orderNo) {
        if(orderService.take(orderNo)) {
            return CommonResult.success();
        }else{
            return CommonResult.failed();
        }
    }

    /**
     * 订单取消
     * @param orderNo Integer 订单id
     */
    @ApiOperation(value = "订单取消")
    @RequestMapping(value = "/cancel", method = RequestMethod.POST)
    public CommonResult<Boolean> cancel(@RequestParam(value = "orderNo") String orderNo) {
        if(orderService.cancel(orderNo)) {
            return CommonResult.success();
        }else{
            return CommonResult.failed();
        }
    }

    /**
     * 商品订单未发货取消
     * @param orderId Integer 订单id
     */
    @ApiOperation(value = "商品订单未发货取消")
    @RequestMapping(value = "/product/cancel", method = RequestMethod.POST)
    public CommonResult<Boolean> productCancel(@RequestParam(value = "orderId") String orderId) {
        if(orderService.productCancel(orderId)) {
            return CommonResult.success();
        }else{
            return CommonResult.failed();
        }
    }

    /**
     * 获取申请订单退款信息
     * @param orderNo 订单编号
     */
    @ApiOperation(value = "获取申请订单退款信息")
    @RequestMapping(value = "/apply/refund/{orderNo}", method = RequestMethod.GET)
    public CommonResult<ApplyRefundOrderInfoResponse> refundApplyOrder(@RequestHeader(value = "language",required = false) String language, @PathVariable String orderNo) {
        return CommonResult.success(orderService.applyRefundOrderInfo(language,orderNo));
    }

    /**
     * 订单退款申请
     * @param request OrderRefundApplyRequest 订单id
     */
    @ApiOperation(value = "订单退款申请")
    @RequestMapping(value = "/refund", method = RequestMethod.POST)
    public CommonResult<Boolean> refundApply(@RequestHeader(value = "language",required = false) String language,@RequestBody @Validated OrderRefundApplyRequest request) {
        if(orderService.refundApply(language,request)) {
            return CommonResult.success();
        }else{
            return CommonResult.failed();
        }
    }


    @ApiOperation(value = "售后详情")
    @RequestMapping(value = "/refund/info", method = RequestMethod.GET)
    public CommonResult<AfterSaleInfoResponse> refundInfo(@RequestHeader(value = "language",required = false) String language,@RequestParam(value = "orderNo") String orderNo) {
       return CommonResult.success(orderService.refundInfo(language,orderNo));
    }


    @ApiOperation(value = "退货下单")
    @RequestMapping(value = "/refund/order", method = RequestMethod.POST)
    public CommonResult refundAnOrder(@RequestBody RefundAnOrderRequest request) {
        return orderService.refundAnOrder(request);
    }

    @ApiOperation(value = "退货下单取消")
    @RequestMapping(value = "/refund/order/cancel", method = RequestMethod.POST)
    public CommonResult refundAnOrderCancel(@RequestParam(value = "orderNo") String orderNo) {
        return orderService.refundAnOrderCancel(orderNo);
    }

    @ApiOperation(value = "修改寄件信息")
    @RequestMapping(value = "/refund/update", method = RequestMethod.POST)
    public CommonResult refundUpdate(@RequestBody RefundAnOrderRequest request) {
        return orderService.refundUpdate(request);
    }

    @ApiOperation(value = "撤销退款退货申请")
    @RequestMapping(value = "/refund/revocation", method = RequestMethod.POST)
    public CommonResult refundRevocation(@RequestParam(value = "orderNo") String orderNo) {
        return orderService.refundRevocation(orderNo);
    }

    @ApiOperation("订单物流查询")
    @RequestMapping(value = "/logistics", method = RequestMethod.GET)
    public CommonResult<ResponseCancel> OrderRouting(@RequestParam(value = "orderNo") String orderNo,
                                                     @RequestParam(value = "type") Integer type){
        return CommonResult.success(sfService.getOrderRouting("",orderNo,type));
    }

    @ApiOperation("获取当前时间戳")
    @RequestMapping(value = "/time", method = RequestMethod.GET)
    public CommonResult<HashMap<String,Long>> getCurrentTime(){
        HashMap<String, Long> stringLongHashMap = new HashMap<>();
        stringLongHashMap.put("time",orderService.getTime());
        return CommonResult.success(stringLongHashMap);
    }
}
